package com.mybatis.service.shop.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.mybatis.basic.entity.AjaxJson;
import com.mybatis.basic.service.impl.BasicServiceImpl;
import com.mybatis.entity.shop.GoodsBrand;
import com.mybatis.service.shop.GoodsBrandService;

@Service
public class GoodsBrandServiceImpl extends BasicServiceImpl<GoodsBrand> implements GoodsBrandService {

	@Override
	public AjaxJson add(GoodsBrand goodsBrand) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			goodsBrand.setCreateTime(new Date());
			goodsBrand.setEditTime(new Date());
			super.insertOne(goodsBrand);
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序错误，添加失败");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson queryOne(Integer id) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			GoodsBrand g = super.selectById(new GoodsBrand(id));
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("查询成功");
			ajaxJson.setObj(g);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序错误，查询失败");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson queryListByPage(GoodsBrand goodsBrand, Integer page, Integer rows) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			//PageHelper.startPage(page, rows);
			List<GoodsBrand> list = super.selectListByPage(page, rows);
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("查询成功");
			ajaxJson.setObj(list);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序错误，查询失败");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson del(Integer id) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			int r = super.deleteById(new GoodsBrand(id));
			ajaxJson.setSuccess(r!=0?true:false);
			ajaxJson.setMsg(r!=0?"删除成功":"删除失败");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序错误，删除失败");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson mod(GoodsBrand goodsBrand) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			int r = super.updateNotNull(goodsBrand);
			ajaxJson.setSuccess(r!=0?true:false);
			ajaxJson.setMsg(r!=0?"删除成功":"删除失败");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序错误，修改失败");
		}
		return ajaxJson;
	}

}
